﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Power.Common;
using JN.Utility;
using DevExpress.XtraTreeList.Nodes;
using System.Net.NetworkInformation;
using Power.BLL;

namespace Power.ComManager
{
    public partial class ComList : DevExpress.XtraEditors.XtraForm
    {
        #region property
        private Com_infoBLL comBLL = new Com_infoBLL();
        #endregion
        public ComList()
        {
            InitializeComponent();
        }

        private void ComList_Load(object sender, EventArgs e)
        {
            //initalize ui
            InitalizeUI();
            //load com port
            LoadPort();
            //load Station;
            LoadStation();
        }
        /// <summary>
        /// 加载Port
        /// </summary>
        private void LoadPort()
        {
            try
            {
                TreeListNode node;
                treeList1.Nodes.Clear();
                node = treeList1.AppendNode(new object[] { "节点列表", "节点列表", null, null }, null);
                node.StateImageIndex = 1;

                //string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                DataTable dtData = comBLL.GetList(" station_id='"+ Manager.SystemStatus.Instance.Station_id +"' ").Tables[0];
                for (int i = 0; i <dtData.Rows.Count; i++)
                {
                    node = treeList1.AppendNode(new object[] { dtData.Rows[i]["com_port"], dtData.Rows[i]["com_port"], null, null }, 0);
                    node.StateImageIndex = 0;
                }
                treeList1.ExpandAll();
                this.treeList1.EndUnboundLoad();

            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                XtraMessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 加载站点
        /// </summary>
        private void LoadStation()
        {
            try
            {
                Station_infoBLL stationBLL = new Station_infoBLL();
                DataTable dtData = stationBLL.GetAllList().Tables[0];
                txtStation.Properties.ValueMember = "station_id";
                txtStation.Properties.DisplayMember = "station_name";
                txtStation.Properties.DataSource = dtData;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
            }
        }
        /// <summary>
        /// 初始化界面
        /// </summary>
        private void InitalizeUI()
        {
            try
            {
                //baud rate
                cmbBaudrate.Properties.Items.Clear();
                cmbBaudrate.Properties.Items.Add("9600");
                cmbBaudrate.Properties.Items.Add("14400");
                cmbBaudrate.Properties.Items.Add("19200");
                cmbBaudrate.Properties.Items.Add("38400");
                cmbBaudrate.Properties.Items.Add("57600");
                cmbBaudrate.SelectedIndex = 0;

                //data bit
                cmbDatabit.Properties.Items.Add("4");
                cmbDatabit.Properties.Items.Add("5");
                cmbDatabit.Properties.Items.Add("6");
                cmbDatabit.Properties.Items.Add("7");
                cmbDatabit.Properties.Items.Add("8");
                cmbDatabit.SelectedIndex = 4;

                //odd even
                cmbOdd.Properties.Items.Add("None");
                cmbOdd.Properties.Items.Add("Odd");
                cmbOdd.Properties.Items.Add("Even");
                cmbOdd.SelectedIndex = 0;

                //stop bit
                cmbStopBit.Properties.Items.Add("1");
                cmbStopBit.Properties.Items.Add("1.5");
                cmbStopBit.Properties.Items.Add("2");
                cmbStopBit.SelectedIndex = 2;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show("初始化界面错误", "错误");
                SystemLogger.Write(ex);
            }
        }

        private void treeList1_Click(object sender, EventArgs e)
        {            
            TreeListNode clickedNode = this.treeList1.FocusedNode;
            string disPlayText = clickedNode.GetDisplayText(0);
            if (disPlayText.ToUpper() != "节点列表")
            {
                //显示text
                DisplayForm(disPlayText);
            }
        }
        private void DisplayForm(string comPort)
        {
            try
            {
                if (string.IsNullOrEmpty(comPort)) return;

                cmbPortNo.Text = comPort;
                Model.Com_info mInfo = comBLL.GetModel(comPort,Manager.SystemStatus.Instance.Station_id);
                if (mInfo != null)
                {
                    cmbBaudrate.Text = mInfo.baud_rate;
                    cmbDatabit.Text = mInfo.data_bit;
                    cmbOdd.Text = mInfo.odd;
                    cmbStopBit.Text = mInfo.stop_bit;
                    txtStation.EditValue = mInfo.station_id;
                    txtCycle.Value = (decimal)mInfo.scan_cycle;
                }

            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.ToString());
                SystemLogger.Write(ex);
            }
        }

        private void btnSet_Click(object sender, EventArgs e)
        {
            if (cmbPortNo.Text == "")
            {
                XtraMessageBox.Show("请选择节点!!","提示");
                return;
            }
            if (SaveData())
            {
                XtraMessageBox.Show("设置成功!!", "提示");
            }
            else
            {
                XtraMessageBox.Show("设置失败!!", "提示");
            }
        }
        /// <summary>
        /// 保存数据到数据库
        /// </summary>
        /// <returns></returns>
        private bool SaveData()
        {
            try
            {
                bool ret = false;
                Model.Com_info mInfo = comBLL.GetModel(cmbPortNo.Text,Manager.SystemStatus.Instance.Station_id);
                if (mInfo != null)
                {
                    //update 
                    mInfo.com_port = cmbPortNo.Text;
                    mInfo.baud_rate = cmbBaudrate.Text;
                    mInfo.data_bit = cmbDatabit.Text;
                    mInfo.odd = cmbOdd.Text;
                    mInfo.stop_bit = cmbStopBit.Text;
                    mInfo.station_id = txtStation.EditValue.ToString();
                    mInfo.scan_cycle = (int)txtCycle.Value;

                    ret = comBLL.Update(mInfo);
                }
                else
                {
                    //add
                    mInfo = new Model.Com_info();
                    mInfo.com_port = cmbPortNo.Text;
                    mInfo.baud_rate = cmbBaudrate.Text;
                    mInfo.data_bit = cmbDatabit.Text;
                    mInfo.odd = cmbOdd.Text;
                    mInfo.stop_bit = cmbStopBit.Text;
                    mInfo.station_id = txtStation.EditValue.ToString();
                    mInfo.scan_cycle = (int)txtCycle.Value;

                    ret = comBLL.Add(mInfo);
                }

                return ret;
            }
            catch (Exception ex)
            {
                SystemLogger.Write(ex);
                return false;
            }
        }
    }
}
